<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="标题"
        style="width:200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.author"
        placeholder="作者"
        class="filter-item"
        style="width:200px"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-select
        v-model="listQuery.category"
        placeholder="分类"
        class="filter-item"
        clearable
        @change="handleFilter"
        @clear="handleFilter"
      >
        <el-option v-for="item in categoryList" :key="item.value" :label="item.label+'('+item.num+')'" :value="item.label" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left:10px"
        @click="handleFilter"
      >查询</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        style="margin-left:5px"
        @click="handleCreate"
      />
      <el-checkbox v-model="showCover" style="margin-left:5px" class="filter-item" @change="changeShowCover">
        显示封面
      </el-checkbox>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width:100%"
      :default-sort="defaultSort"
      @sort-change="sortChange"
    >
      <el-table-column
        label="ID"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      />
      <el-table-column
        label="书名"
        width="150"
        align="center"
      >
        <template v-slot="{row:{titleWrapper}}">
          <span v-html="titleWrapper" />

        </template>
      </el-table-column>
      <el-table-column
        label="作者"
        width="150"
        align="center"
      >
        <template v-slot="{row:{authorWrapper}}">
          <span v-html="authorWrapper" />
        </template>
      </el-table-column>
      <el-table-column prop="publisher" label="出版社" width="150" align="center" />
      <el-table-column prop="category" label="分类" width="150" align="center" />
      <el-table-column prop="publisher" label="出版社" width="150" align="center" />
      <el-table-column prop="language" label="语言" width="150" align="center" />
      <el-table-column
        v-if="showCover"
        label="出版社"
        width="150"
        align="center"
      >
        <template v-slot="{row:{cover}}">
          <a :href="cover" target="_blank">
            <img :src="cover" style="width:120px;height:180px">
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="fileName" label="文件名" width="150" align="center" />
      <el-table-column label="文件路径" width="150" align="center">
        <template v-slot="{row:{filePath}}">
          <span>{{ filePath|valueFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面路径" width="150" align="center">
        <template v-slot="{row:{coverPath}}">
          <span>{{ coverPath|valueFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="解压路径" width="150" align="center">
        <template v-slot="{row:{unzipPath}}">
          <span>{{ unzipPath|valueFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传人" width="150" align="center">
        <template v-slot="{row:{createUser}}">
          {{ createUser | valueFilter }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" width="150" align="center">
        <template v-slot="{row:{createDt}}">
          <span>{{ createDt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template v-slot="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" />
          <el-button type="text" icon="el-icon-delete" style="color:red" @click="handleDelete(row)" />
        </template>

      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="refresh"
    />

  </div>
</template>

<script>
import pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { getCategory, listBook, deleteBook } from '@/api/book'

export default {
  components: {
    pagination
  },
  directives: {
    waves
  },
  filters: {
    valueFilter(value) {
      return value || '无'
    }
  },
  data() {
    return {
      listQuery: {},
      showCover: false,
      categoryList: [],
      tableKey: 0,
      listLoading: true,
      list: [],
      total: 0,
      defaultSort: []
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getCategoryList()
    this.getList()
  },

  methods: {
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      let sort = '+id'
      const listQuery = {
        page: 1,
        pageSize: 20,
        sort
      }
      if (query) {
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.sort && (sort = query.sort)
      }
      const sortSymbol = sort[0]
      const sortColumn = sort.slice(1, sort.length)
      this.defaultSort = {
        prop: sortColumn,
        order: sortSymbol === '+' ? 'ascending' : 'descemdomg'
      }
      this.listQuery = { ...listQuery, ...query }
    },
    sortChange(data) {
      const { order, prop } = data
      this.sortBy(prop, order)
      this.handleFilter()
    },
    sortBy(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sort = `+${prop}`
      } else {
        this.listQuery.sort = `-${prop}`
      }
    },
    wrapperKeyword(k, v) {
      function highlight(value) {
        return `<span style="color:red">${value}</span>`
      }
      if (!this.listQuery[k]) {
        return v
      } else {
        return v.replace(new RegExp(this.listQuery[k], 'ig'), highlight(this.listQuery[k]))
      }
    },
    getList() {
      this.listLoading = true
      listBook(this.listQuery).then(res => {
        const { list, count } = res.data
        this.list = list
        this.listLoading = false
        this.total = count
        this.list.forEach(book => {
          book.titleWrapper = this.wrapperKeyword('title', book.title)
          book.authorWrapper = this.wrapperKeyword('author', book.author)
        })
      })
    },
    getCategoryList() {
      getCategory().then(response => {
        this.categoryList = response.data
      })
    },
    handleUpdate(row) {
      this.$router.push('/book/edit/' + row.fileName)
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该电子书，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBook(row.fileName).then(response => {
          this.$notify({
            title: '图书删除成功',
            message: response.msg || '删除成功',
            type: 'success',
            duration: 2000
          })
          this.handleFilter()
        })
      })
    },
    refresh() {
      this.$router.push({
        path: '/book/list',
        query: this.listQuery
      })
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.refresh()
      // this.getList()
    },
    handleCreate() {
      this.$router.push('/book/create')
    },
    changeShowCover(value) {
      this.showCover = value
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
